<%@ page import="com.example.rk.entity.Admin" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>XXXX后端管理系统</title>
    <jsp:include page="../common_css.jsp"/>

    <style>
        .form-control {
            width: 30%;
        }

    </style>
</head>
<body>
<div class="container-scroller">
    <!-- partial:partials/_navbar.html -->
    <jsp:include page="../top.jsp"/>

    <!-- partial -->
    <div class="container-fluid page-body-wrapper">
        <!-- partial:partials/_sidebar.html -->
        <jsp:include page="./left.jsp"/>
        <!-- partial -->
        <div class="main-panel">
            <div class="row">
                <div class="col-md-12 stretch-card">
                    <div class="card">
                        <div class="card-body">

                            <div class="table-responsive" align="center">
                                <form id="frm" class="form-signin" action="/admin/uploadFile"  method="post" enctype="multipart/form-data">
                                    <h1 class="h3 mb-3 font-weight-normal">添加</h1>
                                    <input  id="uploadFile" type="file" class="form-upload-file" name="image" size="50" >
                                    <button class="btn btn-lg btn-primary btn-block" type="button" onclick="doAdd()"
                                            style="width: 30%">添加
                                    </button>
                                </form>

                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <jsp:include page="../footer.jsp"/>
    </div>
    <!-- main-panel ends -->
</div>
<!-- page-body-wrapper ends -->
</div>
<!-- container-scroller -->
<jsp:include page="../common_js.jsp"/>
</body>
</html>
<script>
    function doAdd() {
        //推荐前端js验证字段
        var file = $("#uploadFile")[0].files[0];
        var formData = new FormData();
        formData.append('file', file);
        $.ajax(
            {
                type: "post",
                url: "/admin/uploadFile",
                data: formData,
                async: false,  //使用ajax同步提交数据
                processData : false,
                contentType : false,
                success: function (response) {
                    if (response == "error") {
                        alert("添加失败");
                    } else if (response == "ok") {
                        alert("添加成功一秒后跳回用户信息界面");
                        setTimeout(window.location.href = "/admin/main", 1000);
                    } else {
                        alert("添加失败，请联系管理员....");
                    }
                }
            }
        );

    }
</script>

